package cn.fintecher.pangolin.service.dataimp.web;

import cn.fintecher.pangolin.common.enums.ExportTemplateType;
import cn.fintecher.pangolin.common.web.BaseController;
import cn.fintecher.pangolin.entity.elastic.ExportTemplate;
import cn.fintecher.pangolin.service.dataimp.model.request.CheckTemplateRequest;
import cn.fintecher.pangolin.service.dataimp.model.response.CheckTemplateResponse;
import cn.fintecher.pangolin.service.dataimp.request.AddExportTempalteRequest;
import cn.fintecher.pangolin.service.dataimp.request.FindExportTemplateRequest;
import cn.fintecher.pangolin.service.dataimp.request.ModifyExportTemplateRequest;
import cn.fintecher.pangolin.service.dataimp.response.FindExportTemplateResponse;
import cn.fintecher.pangolin.common.model.PropertyResponse;
import cn.fintecher.pangolin.service.dataimp.response.TemplateResponse;
import cn.fintecher.pangolin.service.dataimp.service.TemplateCommonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Auther: xiaqun
 * @Description: 导入模板相关接口
 * @Date: 9:42 2019/10/16
 */

@RestController
@RequestMapping("/api/exportTemplateController")
@Api(value = "导入模板相关接口", description = "导入模板相关接口")
public class ExportTemplateController extends BaseController {
    @Autowired
    private TemplateCommonService templateCommonService;

    /**
     * 多条件查询导出模板
     */
    @ApiOperation(value = "多条件查询导出模板", notes = "多条件查询导出模板")
    @GetMapping("/findAllExportTemplate")
    public ResponseEntity<Page<FindExportTemplateResponse>> findAllExportTemplate(FindExportTemplateRequest request, Pageable pageable,
                                                                                  @RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to find all export template,{}", request);
        Page<FindExportTemplateResponse> page = templateCommonService.findAllExportTemplate(request, pageable, token);
        return ResponseEntity.ok().body(page);
    }

    /**
     * 获取导出字段
     */
    @ApiOperation(value = "获取导出字段", notes = "获取导出字段")
    @GetMapping("/getExportProperty")
    public ResponseEntity<List<PropertyResponse>> getExportProperty(@RequestParam @ApiParam(value = "模板类型", required = true) ExportTemplateType type) {
        log.info("REST request to get case property,{}", type);
        List<PropertyResponse> list = templateCommonService.getExportProperty(type);
        return ResponseEntity.ok().body(list);
    }

    /**
     * 新增导出模板
     */
    @ApiOperation(value = "新增导出模板", notes = "新增导出模板")
    @PostMapping("/addExportTemplate")
    public ResponseEntity<Void> addExportTemplate(@Valid @RequestBody AddExportTempalteRequest request,
                                                  @RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to add export template,{}", request);
        templateCommonService.addExportTemplate(request, token);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除导出模板
     */
    @ApiOperation(value = "删除导出模板", notes = "删除导出模板")
    @DeleteMapping("/deleteExportTemplate")
    public ResponseEntity<Void> deleteExportTemplate(@RequestParam @ApiParam(value = "模板ID", required = true) String templateId) {
        log.info("REST request to delete export template,{}", templateId);
        templateCommonService.deleteExportTemplate(templateId);
        return ResponseEntity.ok().build();
    }

    /**
     * 获取导出模板信息
     */
    @ApiOperation(value = "获取导出模板信息", notes = "获取导出模板信息")
    @GetMapping("/getExportTemplateById")
    public ResponseEntity<ExportTemplate> getExportTemplateById(@RequestParam @ApiParam(value = "模板ID", required = true) String templateId) {
        log.info("REST request to get export template by id,{}", templateId);
        ExportTemplate exportTemplate = templateCommonService.getExportTemplateById(templateId);
        return ResponseEntity.ok().body(exportTemplate);
    }

    /**
     * 修改导出模板
     */
    @ApiOperation(value = "修改导出模板", notes = "修改导出模板")
    @PutMapping("/modifyExportTemplate")
    public ResponseEntity<Void> modifyExportTemplate(@Valid @RequestBody ModifyExportTemplateRequest request,
                                                     @RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to modify export template,{}", request);
        templateCommonService.modifyExportTemplate(request, token);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询委托方下导出模板
     */
    @ApiOperation(value = "查询委托方下导出模板", notes = "查询委托方下导出模板")
    @GetMapping("/getExportTemplateByPrincipal")
    public ResponseEntity<List<TemplateResponse>> getExportTemplateByPrincipal(@RequestParam @ApiParam(value = "委托方ID", required = true) String principalId,
                                                                               @RequestParam @ApiParam(value = "模板类型", required = true) ExportTemplateType type) {
        log.info("REST request to get template by principal,{},{}", principalId, type);
        List<TemplateResponse> list = templateCommonService.getExportTemplateByPrincipal(principalId, type);
        return ResponseEntity.ok().body(list);
    }

    /**
     * 解析Excel数据模板头信息
     */
    @ApiOperation(value = "解析Excel数据模板头信息", notes = "解析Excel数据模板头信息")
    @PostMapping(value = "/checkExportTemplate")
    public ResponseEntity<CheckTemplateResponse> checkExportTemplate(@RequestBody CheckTemplateRequest request) throws Exception {
        log.info("REST request to check template,{}", request);
        CheckTemplateResponse response = templateCommonService.checkExportTemplate(request);
        return ResponseEntity.ok().body(response);
    }
}
